using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using DTO;

namespace DAOLayer
{
    public class MatDoSuDungDao
    {
        public static int ThongKeSoNgayOCuaPhongTheoThang(int thang, int map)
        {
            OleDbConnection cn = Database.ConnectionData();
            //'B3: Tao chuoi strSQL thao tac CSDL
            String strSQL = " Select Sum(cthd.SoNgayThue)";
            strSQL += " From PHIEUTHUE pt ,HOADON hd, CHITIETHOADON cthd Where cthd.MaPhieuThue = pt.MaPhieuThue and pt.MaPhong = ? ";
            strSQL += " and cthd.MaHoaDon = hd.MaHoaDon";
            strSQL += " and Month(hd.NgayThanhToan) = ? ";
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            cmd.Parameters.Add("@MaPhong", OleDbType.Integer);
            cmd.Parameters[("@MaPhong")].Value = map;
            cmd.Parameters.Add("@Thang", OleDbType.Integer);
            cmd.Parameters[("@Thang")].Value = thang;
            int sn = 0;           
            sn = Convert.ToInt32(cmd.ExecuteScalar());
            cn.Close();
            return sn;
        }
        public static void Them(MatDoSuDungDto p)
        {
            // B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
            OleDbConnection cn = Database.ConnectionData();
            // B3: Tao chuoi strSQL thao tac CSDL
            string strSQL = "Insert into MATDOSUDUNG ( Thang,MaPhong,SoNgayThue,TyLe ) values(?,?,?, ?)";
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            cmd.Parameters.Add("@Thang", OleDbType.Integer);
            cmd.Parameters["@Thang"].Value = p.Thang;
            cmd.Parameters.Add("@MaPhong", OleDbType.Integer);
            cmd.Parameters["@MaPhong"].Value = p.MaPhong;
            cmd.Parameters.Add("@SoNgayThue", OleDbType.Integer);
            cmd.Parameters["@SoNgayThue"].Value = p.SoNgayThue;
            cmd.Parameters.Add("@TyLe", OleDbType.Double);
            cmd.Parameters["@TyLe"].Value = p.TyLe;
            cmd.ExecuteNonQuery();

            strSQL = "Select @@IDENTITY";
            cmd = new OleDbCommand(strSQL, cn);
            p.MaMatDo = Convert.ToInt32(cmd.ExecuteScalar());
            // B5: Dong ket noi CSDL
            cn.Close();
        }
    }
}
